﻿package emusic.api.clients 
{
	import emusic.ws.RequestWrapper;
	import emusic.api.events.ArtistSearchEvent;
	import emusic.api.feeds.SummaryDataFeed;
	import flash.events.ErrorEvent;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	
	 
	// 	public static const SUCCESS:String = "artist_search_event_success";

	 /**
	 Dispatched by an InteractiveObject continually while a drag gesture remains within its doundary.
	 @eventType	emusic.api.events.ArtistSearchEvent.SUCCESS
	 */
	[Event(name="artist_search_event_success", type="emusic.api.events.ArtistSearchEvent")]
	
	public class ArtistClient extends AbstractEMusicClient
	{
		public function search(params:ArtistSearchRequest):Number
		{
			var request:URLRequest = createRequest(params);
			var wrapper:RequestWrapper = createWrapper( doSearchResult, doSearchError, { request : params } );
			return runLoader(request, wrapper);
		}
		
		protected function doSearchResult(xml:XML, wrapper:RequestWrapper):void
		{
			//trace(xml);
			var feed:SummaryDataFeed = new SummaryDataFeed(xml.artists[0], "artist");
			dispatchEvent(new ArtistSearchEvent(ArtistSearchEvent.SUCCESS, feed, wrapper.pagination, wrapper.requestId));
		}
		
		protected function doSearchError(evt:ErrorEvent, wrapper:RequestWrapper):void
		{
			trace("error:" + evt);
		}
	}
}